import sys

# 迭代器与生成器
list1 = [1, 2, 3, 4]
it = iter(list1)
print(next(it))
print(next(it))
print(next(it))
print(next(it))

list2 = [9, 8, 7, 6]
it = iter(list2)  # 创建迭代器对象
for x in it:
    print(x, end=" ")


# 生成器函数 - 斐波那契
def fibonacci(n):
    a, b, counter = 0, 1, 0
    while True:
        if (counter > n):
            return
        yield a
        a, b = b, a + b
        counter += 1


f = fibonacci(10)  # f 是一个迭代器，由生成器返回生成


# while True:
#     try:
#         print(next(f), end=" ")
#     except StopIteration:
#         sys.exit()


# 函数
def print_welcome(name):
    print("Welcome", name)


print_welcome("Runoob")


# 必须参数
def printme1(str):
    "打印任何传入的字符串"
    print(str)


# 调用printme函数
printme1('123')


# 关键字参数
def printme2(str):
    "打印任何传入的字符串"
    print(str)


# 调用printme函数
printme2(str="菜鸟教程")


# 关键字参数顺序不重要
def printinfo1(name, age):
    "打印任何传入的字符串"
    print("名字: ", name)
    print("年龄: ", age)


# 调用printinfo函数
printinfo1(age=50, name="runoob")


# 默认参数
def printinfo2(name, age=35):
    "打印任何传入的字符串"
    print("名字: ", name)
    print("年龄: ", age)


# 调用printinfo函数
printinfo2(age=50, name="runoob")
print("------------------------")
printinfo2(name="runoob")


# 不定长参数  为元组
def printinfo3(arg1, *vartuple):
    "打印任何传入的参数"
    print("输出: ")
    print(arg1)
    for var in vartuple:
        print(var)


# 调用printinfo 函数
printinfo3(10)
printinfo3(70, 60, 50)

# lambda表达式
sum = lambda arg1, arg2: arg1 + arg2

# 调用sum函数
print("相加后的值为 : ", sum(10, 20))


# return语句
def sum(arg1, arg2):
    # 返回2个参数的和."
    total = arg1 + arg2
    print("函数内 : ", total)
    return total


# 调用sum函数
total = sum(10, 20)
print("函数外 : ", total)

# 全局变量
num = 1
def fun1():
    global num  # 需要使用 global 关键字声明
    print(num)
    num = 123
    print(num)
fun1()